package com.ruoyi.parkingcar.controller;

import java.io.File;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.parkingcar.domain.ParkCar;
import com.ruoyi.parkingcar.domain.ParkCarAndWhitelist;
import com.ruoyi.parkingcar.domain.ParkCarUser;
import com.ruoyi.parkingcar.util.AliyunOcrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.parkingcar.domain.ParkWhitelist;
import com.ruoyi.parkingcar.service.IParkWhitelistService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆白名单管理Controller
 * 
 * @author 饼干
 * @date 2025-02-08
 */
@RestController
@RequestMapping("/parkingcar/whitelist")
public class ParkWhitelistController extends BaseController
{
    @Autowired
    private IParkWhitelistService parkWhitelistService;

    @Value("${file.imgFile}")
    private String path;

    @Autowired
    private AliyunOcrUtil aliyunOcrUtil;

    /**
     * 查询车辆白名单管理列表
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:whitelist:list')")
    @GetMapping("/list")
    public AjaxResult list(ParkWhitelist parkWhitelist)
    {
        startPage();
        List<ParkCarAndWhitelist> list = parkWhitelistService.selectParkWhitelistList(parkWhitelist);
        List<ParkCarUser> parkCarList = parkWhitelistService.selectParkCarUserList();
        return AjaxResult.success("成功").put("whiteList",getDataTable(list)).put("parkCarList",parkCarList);
    }

    /**
     * 导出车辆白名单管理列表
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:whitelist:export')")
    @Log(title = "车辆白名单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParkWhitelist parkWhitelist)
    {
        List<ParkCarAndWhitelist> list = parkWhitelistService.selectParkWhitelistList(parkWhitelist);
        ExcelUtil<ParkCarAndWhitelist> util = new ExcelUtil<ParkCarAndWhitelist>(ParkCarAndWhitelist.class);
        util.exportExcel(response, list, "车辆白名单管理数据");
    }

    /**
     * 获取车辆白名单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:whitelist:query')")
    @GetMapping(value = "/{whitelistId}")
    public AjaxResult getInfo(@PathVariable("whitelistId") Long whitelistId)
    {
        return success(parkWhitelistService.selectParkWhitelistByWhitelistId(whitelistId));
    }

    /**
     * 新增车辆白名单管理
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:whitelist:add')")
    @Log(title = "车辆白名单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParkWhitelist parkWhitelist)
    {
        return toAjax(parkWhitelistService.insertParkWhitelist(parkWhitelist));
    }

    /**
     * 修改车辆白名单管理
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:whitelist:edit')")
    @Log(title = "车辆白名单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParkWhitelist parkWhitelist)
    {
        return toAjax(parkWhitelistService.updateParkWhitelist(parkWhitelist));
    }

    /**
     * 删除车辆白名单管理
     */
    @PreAuthorize("@ss.hasPermi('parkingcar:whitelist:remove')")
    @Log(title = "车辆白名单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{whitelistIds}")
    public AjaxResult remove(@PathVariable Long[] whitelistIds)
    {
        return toAjax(parkWhitelistService.deleteParkWhitelistByWhitelistIds(whitelistIds));
    }

    //查询所有车牌号
    @GetMapping("/car")
    public AjaxResult car(){
        List<ParkWhitelist> carList = parkWhitelistService.selectLicensePlate();
        return AjaxResult.success(carList);
    }

    //车牌识别
    @PostMapping("/IdNumberImg")
    public AjaxResult IdNumberImg(MultipartFile imgFile) throws Exception {
        System.out.println(imgFile);
        if (imgFile.isEmpty()) {
            return AjaxResult.error("上传文件为空");
        }

//        //保存得路径位置
//        String path = "D:/桌面/图片";

        String filePath = path + "/" + imgFile.getOriginalFilename();

        //将上传文件保存到目录下
        imgFile.transferTo(new File(filePath));

        //调用阿里云识别
        Map<String, Object> result = aliyunOcrUtil.method(filePath);

        if (result == null) {
            return AjaxResult.error("识别失败");
        }

        result.put("filePath",filePath);
        return  AjaxResult.success(result);
    }
}
